<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="assets/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/right.css" rel="stylesheet">
    <script src="assets/jquery-3.5.1.min.js"></script>
    <script src="assets/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
    <script src="assets/vue.min-v2.5.16.js"></script>
    <script src="assets/vue-router.min-2.7.0.js"></script>
    <script src="assets/axios.min.js"></script>
</head>
<body>
    <div id="app" class="container">
        <div class="row">
            <div class="col-md-12" style="height: 50px; line-height: 50px;">
                <button class="btn btn-info" @click="doAdd">新增</button>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <table class="table table-striped">
                    <caption>用户列表</caption>
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>姓名</th>
                        <th>类型</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                        <tr v-for="u in userList">
                            <td>{{u.id}}</td>
                            <td>{{u.username}}</td>
                            <td>{{u.type==1?'超级管理员':'普通用户'}}</td>
                            <td>
                                <button class="btn btn-primary" @click="doUpdate(u.id)">修改</button>
                                <button class="btn btn-danger" @click="doRemove(u.id)">删除</button>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <ul class="pagination" v-for="p in pageNum">
                    <li v-if="p == pageIndex" class="active"><a @click="doGo(p)">{{p}}</a></li>
                    <li v-if="p != pageIndex"><a @click="doGo(p)">{{p}}</a></li>
                </ul>
            </div>
        </div>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                pageIndex:1,
                pageSize:6,
                pageNum:0, //页数
                userList:null
            },
            methods: {
                doGo(p){
                    this.pageIndex=p;
                    var uid = localStorage.getItem("uid");
                    var url='http://localhost:8080/user/list?pageIndex='+this.pageIndex+'&pageSize='+this.pageSize+'&uid='+uid;
                    axios.get(url).then(response =>{
                        if(response.data.data!=null){
                            this.userList=response.data.data;
                            this.pageNum=Math.ceil(response.data.pageTotal/this.pageSize);
                        }else{
                            this.userList=null;
                            this.pageNum=0;
                            alert("没有符合条件的用户");
                        }
                    });
                },
                doAdd(){
                    window.parent.main_right.location.href="user_add_update.html";
                },
                doUpdate(id){
                    window.parent.main_right.location.href="user_add_update.html?id="+id;
                },
                doRemove(id){
                    var uid = localStorage.getItem("uid");
                    var url='http://localhost:8080/user/deleteById?id='+id+'&uid='+uid;
                    axios.get(url).then(response =>{
                        alert(response.data.msg);
                        this.doGo(this.pageIndex);
                    });
                 }

            },
            created: function () {
                this.doGo(1);
            }
        });
    </script>
</body>
</html>